import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

// This class will return all the courses that the user has added.
// This class will get called everytime the user logs in.
public class IsTaking extends HttpServlet{
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/plain");
		PrintWriter pw = response.getWriter();
		String userId = request.getParameter("userId");
		//connect to the database
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		int result = 0;
		String query = "SELECT DISTINCT course_code from course_enrolled where user_name = \""+userId+"\"";
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			con = DriverManager.getConnection("jdbc:mysql://bungle08.cs.toronto.edu:3306/group22",
			"group22", "123456");
	
			if(!con.isClosed()){
				stmt = con.createStatement();		
				rs = stmt.executeQuery(query);			
					
				while (rs.next()) {
					pw.println(rs.getString("course_code"));
					
				}
			}
		}
		catch(Exception e) {
			pw.println("Exception: " + e.getMessage());
		} finally {
			try {
				if(con != null)
					con.close();
			} catch(SQLException e) {}
		}
	}
}